package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.ShopMapper;
import com.example.zqh.mapper.UserMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Food;
import com.example.zqh.mapper.FoodMapper;
import com.example.zqh.pojo.Shop;
import com.example.zqh.pojo.User;
import com.example.zqh.pojo.params.FoodParams;
import com.example.zqh.pojo.params.OneStringOneNumber;
import com.example.zqh.pojo.params.TwoInt;
import com.example.zqh.service.FoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import com.example.zqh.utils.es.EsFoodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Autowired
    FoodMapper foodMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    EsFoodUtils esFoodUtils;





    /**
      * @Author: zqh
      * @Date:
      * @Description: 店铺添加新菜品
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean addNewDishesToTheStore(FoodParams foodParams, Principal principal)throws IOException {
        String name = principal.getName();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        AssertUtil.isTrue(user == null,"参数错误");

        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("user_id", user.getId()));
        AssertUtil.isTrue(shop == null,"你还没有开店，无法上传商品");

        Food food = new Food();
        food.setName(foodParams.getFoodName())
                .setIngredientList(foodParams.getIngredientList())
                .setPrice(foodParams.getPrice())
                .setFoodDesc(foodParams.getFoodDesc())
                .setBenefit(foodParams.getBenefit())
                .setPic(foodParams.getPic())
                .setFoodType(0)
                .setShopId(shop.getId());
        boolean b = esFoodUtils.addOne(food);
        AssertUtil.isTrue(b != true,"上传菜品失败");
        int insert = foodMapper.insert(food);
        AssertUtil.isTrue(insert != 1,"上传菜品失败");

        return RespBean.success("上传菜品成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 下架菜品
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean disher(Integer id) throws IOException{
        boolean b = esFoodUtils.deleteOne(id);
        AssertUtil.isTrue(b != true, "下架菜品失败");
        int i = foodMapper.deleteById(id);
        AssertUtil.isTrue(i != 1,"下架菜品失败");
        return RespBean.success("下架菜品成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改菜品名
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheDishName(OneStringOneNumber oneStringOneNumber)throws IOException {
        Food food = foodMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(food == null,"参数传递错误");
        food.setName(oneStringOneNumber.getText());
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"菜名修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1,"菜名修改失败");
        return RespBean.success("菜名修改成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改菜品原理
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheIngredientsOfDishes(OneStringOneNumber oneStringOneNumber)throws IOException {
        Food food = foodMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(food == null,"参数传递错误");
        food.setIngredientList(oneStringOneNumber.getText());
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1,"修改菜品原料失败");
        return RespBean.success("修改菜品原料成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改菜品的价格
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyThePriceOfADish(TwoInt twoInt)throws IOException {
        Food food = foodMapper.selectById(twoInt.getId());
        food.setPrice(Double.valueOf(twoInt.getPrice()));
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1, "修改菜品价格失败");
        return RespBean.success("修改菜品价格成功");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheDescriptionOfTheDish(OneStringOneNumber oneStringOneNumber)throws IOException {
        Food food = foodMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(food == null,"参数传递错误");
        food.setFoodDesc(oneStringOneNumber.getText());
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1,"修改菜品描述失败");
        return RespBean.success("修改菜品描述成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改菜品吃了以后的好处
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean theBenefitsOfModifyingTheDishesAfterEating(OneStringOneNumber oneStringOneNumber)throws IOException {
        Food food = foodMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(food == null,"参数传递错误");
        food.setBenefit(oneStringOneNumber.getText());
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1,"修改吃了以后的好处失败");
        return RespBean.success("修改成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改菜品图片
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyPicturesOfDishes(OneStringOneNumber oneStringOneNumber)throws IOException {
        Food food = foodMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(food == null,"参数传递错误");
        food.setPic(oneStringOneNumber.getText());
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"修改失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1, "修改菜品图片失败");
        return RespBean.success("修改菜品图片成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询某个商店菜品的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getShopFoodNumberByShop_id(Integer shop_id) {
        Integer number = foodMapper.getNumberByShop_id(shop_id);
        return RespBean.success("查询成功",number);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 分页查询菜品
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getFoodsByPage(Integer shop_id, Integer page, Integer size)throws IOException {
        page = (page - 1) * size;
        List<Food> foods = esFoodUtils.selectListByShopId(shop_id, page, size);
//        List<Food> foods = foodMapper.searchOurDishesByStoreNameAndPage(shop_id, page, size);
        return RespBean.success("查询成功",foods);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过菜品id查询某个菜品的信息
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean queryTheInformationOfADishByTheDishId(Integer id) {
        Food food = foodMapper.selectById(id);
        return RespBean.success("查询成功",food);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 分页查询所有菜品
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getAllFoodsByPaging(Integer page, Integer size) throws IOException{
        page = (page - 1)*size;
        List<Food> foods = esFoodUtils.selectAllList(page, size);
//        List<Food> foods = foodMapper.getFoodsByPag((page - 1) * size, size);
        return RespBean.success("查询成功",foods);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有菜品的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getTheQuantityOfAllDishes() {
        Integer allNumber = foodMapper.getAllNumber();
        return RespBean.success("查询成功",allNumber);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过食物类型，分页查询食物
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getFoodsByTypeAndPage(Integer typeId, Integer page, Integer size)throws IOException {
        page = (page - 1) * size;
        List<Food> foods = esFoodUtils.selectListByTypeId(typeId, page, size);
       // List<Food> foods = foodMapper.getFoodsByTp(typeId, (page - 1) * size, size);
        return RespBean.success("查询成功",foods);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过食物种类id，获取该种种类的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getNumberByType(Integer typeId) {
        Integer numer = foodMapper.getFoodNumerByType(typeId);
        return RespBean.success("查询成功",numer);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 设置某个菜品为热销菜品
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean setADishAsAPopularDish(Integer id) throws IOException {
        Food food = foodMapper.selectById(id);
        AssertUtil.isTrue(food == null,"参数传递错误");
        AssertUtil.isTrue(food.getFoodType() == 0,"该食物已经是热销了");
        food.setFoodType(0);
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"设置失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i!=1,"设置失败");
        return RespBean.success("设置成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 把菜品设置为滞销菜品
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean setDishesAsSlowSellingDishes(Integer id)throws IOException {
        Food food = foodMapper.selectById(id);
        AssertUtil.isTrue(food == null,"参数传递错误");
        AssertUtil.isTrue(food.getFoodType() == 1,"该食物已经是滞销了");
        food.setFoodType(1);
        boolean b = esFoodUtils.updateOne(food);
        AssertUtil.isTrue(b != true,"设置失败");
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i!=1,"设置失败");
        return RespBean.success("设置成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 创建food的es数据库
    */
    @Override
    @Transactional
    public RespBean createFoodSEsDatabase() throws IOException {
        boolean b = esFoodUtils.CreateIndex();
        AssertUtil.isTrue(b != true,"创建失败");
        return RespBean.success("创建成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 删除food的es数据库
    */
    @Override
    @Transactional
    public RespBean deletefoodEsDatabase() throws IOException {
        boolean b = esFoodUtils.DeleteIndex();
        AssertUtil.isTrue(b != true,"删除es表失败");
        return RespBean.success("删除成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 判断Food的es数据库是否存在
    */
    @Override
    @Transactional
    public RespBean GetIndex() throws IOException {
        boolean b = esFoodUtils.GetIndex();
        AssertUtil.isTrue(b != true,"不存在");
        return RespBean.success("存在");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: song的所有数据库上传es里面
    */
    @Override
    @Transactional
    public RespBean allDatabasesOfFoodAreUploadedInEs() throws IOException {
        List<Food> foods = foodMapper.selectList(null);
        boolean b = esFoodUtils.addAll(foods);
        AssertUtil.isTrue(b != true,"输出数据到es失败");
        return RespBean.success("上传数据到es成功");
    }

    @Override
    @Transactional
    public RespBean searchEngine(String keyword) throws IOException {
        List<String> strings = esFoodUtils.getAutocompletionList(keyword);
        return RespBean.success("查询成功",strings);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过名字查询多条
    */
    @Override
    @Transactional
    public RespBean searchByName(String name) throws IOException {
        List<Food> foods = esFoodUtils.selectBySuggest(name);
        return RespBean.success("查询成功",foods);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 管理员修改菜品信息接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean changeFoodByAdmin(Food food) {
        int i = foodMapper.updateById(food);
        AssertUtil.isTrue(i != 1,"修改菜品信息失败");
        return RespBean.success("修改菜品信息成功");
    }


}
